from utils.driver_utils import driver_utils
from utils.logger_utils import logger


class BasePage:
    def __init__(self):
        self.driver = driver_utils.get_driver()
        self.logger = logger

    def find_element(self, locator):
        """查找单个元素"""
        try:
            element = self.driver.find_element(*locator)
            self.logger.info(f"找到元素: {locator}")
            return element
        except Exception as e:
            self.logger.error(f"未找到元素: {locator}, 错误: {e}")
            driver_utils.take_screenshot(f"element_not_found_{locator[1]}")
            raise

    def find_elements(self, locator):
        """查找多个元素"""
        try:
            elements = self.driver.find_elements(*locator)
            self.logger.info(f"找到 {len(elements)} 个元素: {locator}")
            return elements
        except Exception as e:
            self.logger.error(f"查找元素失败: {locator}, 错误: {e}")
            driver_utils.take_screenshot(f"elements_not_found_{locator[1]}")
            raise

    def click(self, locator):
        """点击元素"""
        element = self.find_element(locator)
        try:
            element.click()
            self.logger.info(f"点击元素: {locator}")
        except Exception as e:
            self.logger.error(f"点击元素失败: {locator}, 错误: {e}")
            driver_utils.take_screenshot(f"click_failed_{locator[1]}")
            raise

    def send_keys(self, locator, value, clear_first=True):
        """在元素中输入文本"""
        element = self.find_element(locator)
        try:
            if clear_first:
                element.clear()
            element.send_keys(value)
            self.logger.info(f"在元素 {locator} 中输入: {value}")
        except Exception as e:
            self.logger.error(f"输入文本失败: {locator}, 错误: {e}")
            driver_utils.take_screenshot(f"send_keys_failed_{locator[1]}")
            raise

    def get_text(self, locator):
        """获取元素文本"""
        element = self.find_element(locator)
        try:
            text = element.text
            self.logger.info(f"获取元素 {locator} 的文本: {text}")
            return text
        except Exception as e:
            self.logger.error(f"获取文本失败: {locator}, 错误: {e}")
            driver_utils.take_screenshot(f"get_text_failed_{locator[1]}")
            raise

    def is_element_displayed(self, locator):
        """判断元素是否显示"""
        try:
            element = self.find_element(locator)
            displayed = element.is_displayed()
            self.logger.info(f"元素 {locator} 是否显示: {displayed}")
            return displayed
        except Exception:
            self.logger.info(f"元素 {locator} 未显示")
            return False

    def wait_for_element(self, locator, timeout=10):
        """显式等待元素出现"""
        from selenium.webdriver.support.ui import WebDriverWait
        from selenium.webdriver.support import expected_conditions as EC

        try:
            WebDriverWait(self.driver, timeout).until(
                EC.presence_of_element_located(locator)
            )
            self.logger.info(f"元素 {locator} 已出现")
        except Exception as e:
            self.logger.error(f"等待元素超时: {locator}, 错误: {e}")
            driver_utils.take_screenshot(f"wait_element_timeout_{locator[1]}")
            raise
